import os
import cv2
import numpy as np
import random

# 指定图片文件夹路径
image_folder = 'save'

# 遍历文件夹中的所有图片
for filename in os.listdir(image_folder):
    if filename.endswith(".jpg") or filename.endswith(".png"):
        # 读取图像
        image_path = os.path.join(image_folder, filename)
        img = cv2.imread(image_path)

        # 获取图像的高度和宽度
        height, width, _ = img.shape

        # 生成随机位置和大小
        # x = random.randint(0, width)
        # y = random.randint(0, height)
        # w = random.randint(50, 100)  # 随机宽度
        # h = random.randint(50, 100)  # 随机高度
        x = 100
        y = 150
        w = 200
        h = 150

        # 将选定的区域设置为黑色
        img[y:y+h, x:x+w] = [0, 0, 0]

        # 保存修改后的图像
        cv2.imwrite(os.path.join('new_save/', filename), img)